package com.huiquan.icd.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.icd.service.IcdDiseaseWeatherService;

/**
 * 疾病与天气 controller
 *
 * @author leo
 * @time 2017年4月7日 下午2:02:18
 */
@RequestMapping("/icd/disease_weather")
@Controller
public class IcdDiseaseWeatherController {

	@Autowired
	private IcdDiseaseWeatherService service;

	/**
	 * 查看疾病与天气列表
	 * 
	 * @param req
	 * @param resp
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/view_disease_weather_list.htm")
	public ModelAndView viewDiseaseWeatherList(HttpServletRequest req, HttpServletResponse resp) {
		String tabid = req.getParameter("tabid");
		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("status", "1");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}
		int status = Integer.parseInt(RequestParamUtil.getParamByRequestOrSession(req, pageParam, "status"));
		int start = Integer.parseInt(RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start"));
		String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		Map<String, Object> map = service.viewDiseaseWeatherList(start, keyword, status, countPerPageStr);
		return new ModelAndView("/icd/index", map);
	}

	/**
	 * 更新数据
	 * 
	 * @param req
	 * @param resp
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/update_data.do")
	public ReturnData updateData(HttpServletRequest req, HttpServletResponse resp) {
		User user = (User) req.getSession().getAttribute("user");
		Map<String, String> params = new HashMap<>();
		int status = RequestParamUtil.getIntegerDefaultDi(req, "status", 1);
		params.put("id", req.getParameter("id"));
		if (status == 1) {
			params.put("season", RequestParamUtil.getTrimString(req, "season"));
			params.put("disease", RequestParamUtil.getTrimString(req, "disease"));
			params.put("priority", RequestParamUtil.getTrimString(req, "priority"));
			params.put("age", RequestParamUtil.getTrimString(req, "age"));
		} else if (status == 2) {
			params.put("season", RequestParamUtil.getTrimString(req, "season"));
			params.put("ruleType", RequestParamUtil.getTrimString(req, "ruleType"));
			params.put("subType", RequestParamUtil.getTrimString(req, "subType"));
			params.put("value", RequestParamUtil.getTrimString(req, "value"));
			params.put("disease", RequestParamUtil.getTrimString(req, "disease"));
			params.put("priority", RequestParamUtil.getTrimString(req, "priority"));
			params.put("age", RequestParamUtil.getTrimString(req, "age"));
			params.put("tips", RequestParamUtil.getTrimString(req, "tips"));
		} else if (status == 3) {
			params.put("age1", RequestParamUtil.getTrimString(req, "age1"));
			params.put("age2", RequestParamUtil.getTrimString(req, "age2"));
			params.put("sex", RequestParamUtil.getTrimString(req, "sex"));
			params.put("month", RequestParamUtil.getTrimString(req, "month"));
			params.put("di", RequestParamUtil.getTrimString(req, "di"));
			params.put("rank", RequestParamUtil.getTrimString(req, "rank"));
		}
		return service.updateData(user, params, status);
	}

	/**
	 * 导入文件
	 * 
	 * @param req
	 * @param resp
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("/import_data.do")
	public ReturnData importData(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(required = false) MultipartFile file) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		int status = RequestParamUtil.getIntegerDefaultDi(req, "status", 1);
		return service.importData(user, file, status);
	}

	/**
	 * 导出文件
	 * 
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	@RequestMapping("/export_data.do")
	public void exportData(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		int status = RequestParamUtil.getIntegerDefaultDi(req, "status", 1);
		service.exportData(resp, status);
	}

	/**
	 * 删除一条数据
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("/delete_data.do")
	public ReturnData deleteData(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		int status = RequestParamUtil.getIntegerDefaultDi(req, "status", 1);
		int id = RequestParamUtil.getIntegerDefaultDi(req, "id", 0);
		return service.deleteData(id, status);
	}

	/**
	 * 添加一条数据
	 * 
	 * @param req
	 * @param resp
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/add_data.do")
	public ReturnData addtData(HttpServletRequest req, HttpServletResponse resp) {
		User user = (User) req.getSession().getAttribute("user");
		int status = RequestParamUtil.getIntegerDefaultDi(req, "status", 1);
		Map<String, String> param = new HashMap<>();
		if (status == 1) {
			param.put("season", RequestParamUtil.getTrimString(req, "season"));
			param.put("disease", RequestParamUtil.getTrimString(req, "disease"));
			param.put("priority", RequestParamUtil.getTrimString(req, "priority"));
			param.put("age", RequestParamUtil.getTrimString(req, "age"));
		} else if (status == 2) {
			param.put("season", RequestParamUtil.getTrimString(req, "season"));
			param.put("ruleType", RequestParamUtil.getTrimString(req, "ruleType"));
			param.put("subType", RequestParamUtil.getTrimString(req, "subType"));
			param.put("value", RequestParamUtil.getTrimString(req, "value"));
			param.put("disease", RequestParamUtil.getTrimString(req, "disease"));
			param.put("priority", RequestParamUtil.getTrimString(req, "priority"));
			param.put("age", RequestParamUtil.getTrimString(req, "age"));
			param.put("tips", RequestParamUtil.getTrimString(req, "tips"));
		} else if (status == 3) {
			param.put("age1", RequestParamUtil.getTrimString(req, "age1"));
			param.put("age2", RequestParamUtil.getTrimString(req, "age2"));
			param.put("sex", RequestParamUtil.getTrimString(req, "sex"));
			param.put("month", RequestParamUtil.getTrimString(req, "month"));
			param.put("di", RequestParamUtil.getTrimString(req, "di"));
			param.put("rank", RequestParamUtil.getTrimString(req, "rank"));
		}
		return service.addData(status, param, user);
	}
}
